<?php

require_once "../com/dtos/Accion.php";
include("../DB.php");
//conection info
class Acciones {

    public function getListaConFiltro($pendiente) {
    	$query = "SELECT * from ACC_ACCIONES WHERE PNDPK = $pendiente";
        $result = mysql_query($query);
        $accion = null;
        $out = array();
        while ($row = mysql_fetch_assoc($result)) {
	        $accion = new Accion();
	    	$accion->pk=$row['ACCPK'];
	    	$accion->accion = $row['ACCACCION'];
	    	$accion->cierra = $row['ACCCIERRA'];
			$out[] = $accion;
        }
    	return $out;
    }

    public function nuevo($llegada){
		$accion = (object) $llegada;
    	$query = "INSERT INTO ACC_ACCIONES (ACCACCION, ACCCIERRA, ACCFECHA, ACCOBSERVACIONES, USRPK, PNDPK) ".
				 "VALUES ('$accion->accion', " .
				 "$accion->cierra, " .
    			 "'$accion->fecha'," .
    			 "'$accion->observaciones'," .
    			 "$accion->usrPk," .
    			 "$accion->pndPk) ";
    	mysql_query($query);
    	if (mysql_affected_rows() == 1){
    		$query = "UPDATE PND_PENDIENTES SET ";
    		if ($accion->cierra == 1){
    			$query = $query."PNDSTATUS = 'Resuelto' ";
    		}else{
    			$query = $query."PNDSTATUS = 'Atendiendo' ";
    		}
    			$query = $query."WHERE PNDPK = $accion->pndPk";
    		mysql_query($query);
    		if (mysql_affected_rows() == 1){
    			return;
    		}else{
    			throw new exception("Error al cambiar el estatus\n".mysql_error()."\n".$query);
    		}
    	}else{
    		throw new exception("Error al agregar\n".mysql_error()."\n".$query);
    		//throw new exception($query);
    	}
    }

    public function borrar($pk){
    	$query = "delete from ACC_ACCIONES " .
    			"WHERE ACCPK = $pk ";
    	mysql_query($query);
    	if (mysql_affected_rows() == 1){
    		return ;
    	}else{
    		throw new exception("Error al borrar\n".mysql_error());
    	}
    }

    public function consultar($pk) {
		$query = "SELECT * from ACC_ACCIONES ACC inner join PND_PENDIENTES PND ON ACC.PNDPK = PND.PNDPK  " .
				"where ACCPK = '$pk'";
        $result = mysql_query($query);
        $pendiente = null;
        if (mysql_num_rows($result) > 0){
        	$row = mysql_fetch_array($result);
	        $accion = new Accion();
	    	$accion->pk=$row['ACCPK'];
	    	$accion->accion = $row['ACCACCION'];
	    	$accion->fecha = $row['ACCFECHA'];
	    	$accion->observaciones = $row['ACCOBSERVACIONES'];
	    	$accion->cierra = $row['ACCCIERRA'];
	    	$accion->pndPk = $row['PNDPK'];
	    	$accion->pndDescripcion = $row['PNDDESCRIPCION'];
        }else{
        	throw new Exception("Pendiente no válido");
        }
    	return $accion;
    }

    public function edit($llegada){
		$accion = (object) $llegada;
    	$query = "UPDATE ACC_ACCIONES SET " .
    			"ACCACCION = '$accion->accion', " .
		    	"ACCOBSERVACIONES = '$accion->observaciones', " .
		    	"ACCCIERRA = '$accion->cierra' ".
		    	"WHERE ACCPK = $accion->pk";
    	mysql_query($query);
    	if (mysql_affected_rows() == 1){
    		return ;
    	}else{
    		throw new exception("Error al editar\n".mysql_error());
    		//throw new exception($query);
    	}
    }
}
?>